/**
 * @author hoang.pt
 * @version $Id: Thread2.java 175 2010-09-10 03:50:49Z phantichhoang $
 */

package test1.problem2;

/**
 *
 * @author hoang.pt
 */
public class Thread2 implements Runnable{

    Thread t;

    Data d;

    /**
     * constructor
     */
    public Thread2(Data d) {

        //use data with thread 1
        this.d = d;

        //init thread 2
        t = new Thread(this);
        t.start();
    }

    
    /**
     * 
     */
    public void run()
    {


       //run forever
       while (true) {
            //synchronise
           synchronized(d) {

               if (d.flag) {
                   //display name
                   d.displayName();

                   //change flag
                   d.flag = false;

                   //notify
                   d.notify();
               }

               //wait if busy
               try {
                    d.wait();
                } catch (InterruptedException ex) {
                }
           }
       }
    }
}
